//
//  YJAddressPickerView.m
//  YJB
//
//  Created by 韦朝宾 on 16/5/27.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "YJAddressPickerView.h"
#import "YJDistrictModel.h"
#define PICKERVIEWH  216
//工具条高度
#define TOOLBARH 44
@interface YJAddressPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,weak)UIPickerView *pickerView;
@property(nonatomic,strong)NSDictionary *dict;
@property(nonatomic,strong)NSArray *districts;
@property(nonatomic,weak)UIView *toolBar;
//城市
@property(nonatomic,strong)NSArray *cities;
//县
@property(nonatomic,strong)NSArray *counties;


@end

@implementation YJAddressPickerView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        
        //顶部工具条
        UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, IPHONE_H, IPHONE_W, TOOLBARH)];
        toolBar.backgroundColor = [UIColor whiteColor];
        [self addSubview:toolBar];
        _toolBar = toolBar;
        //取消
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 50, toolBar.height);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:APPCOLOR forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:cancelBtn];
        
        //确认
        UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        completeBtn.frame = CGRectMake(toolBar.width - 50, 0, 50, toolBar.height);
        [completeBtn setTitle:@"完成" forState:UIControlStateNormal];
        [completeBtn setTitleColor:APPCOLOR forState:UIControlStateNormal];
        [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:completeBtn];
        
        //标题
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(cancelBtn.width, 0, toolBar.width - 2 *cancelBtn.width, toolBar.height)];
        label.text = @"选择所在地区";
        label.font = [UIFont systemFontOfSize:18.0f];
        label.textAlignment = NSTextAlignmentCenter;
        [toolBar addSubview:label];
        
        //分割线
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, toolBar.height - 1, toolBar.width, 1)];
        line.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
        [toolBar addSubview:line];
        
        //选择器
        UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, IPHONE_H + TOOLBARH, IPHONE_W, PICKERVIEWH)];
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:pickerView];
        _pickerView = pickerView;
        
    }
    return self;
}

-(NSArray *)districts
{
    if (_districts == nil)
    {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"districtMGE.plist" ofType:nil];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
        _districts = [YJDistrictModel districtsWithArray:dict[@"Division"]];
    }
    return _districts;
}

#pragma mark ----------------------- datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

//每一列行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (component == 0)//省
    {
       return self.districts.count;
    }
    else if (component == 1)//市
    {
        NSInteger selectProvinceIndex = [pickerView selectedRowInComponent:0];
        YJDistrictModel *province = self.districts[selectProvinceIndex];
        //必须在这里取出并存好所有城市
        self.cities = province.cities;
        return province.cities.count;
    }else//县
      {

          NSInteger selectProvinceIndex = [pickerView selectedRowInComponent:0];
          YJDistrictModel *provinceModel = self.districts[selectProvinceIndex];
          
          NSInteger selectCityIndex = [pickerView selectedRowInComponent:1];
          YJCityModel *cityModel = provinceModel.cities[selectCityIndex];
          //取出所有的县城
          self.counties = cityModel.counties;
          return cityModel.counties.count;
    }
}

//每一列的View
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    UILabel *label = [UILabel new];
    label.font = [UIFont systemFontOfSize:20.0f];
    label.textAlignment = NSTextAlignmentCenter;
    if (component == 0)
    {
        YJDistrictModel *districtModel = self.districts[row];
       label.text = districtModel.province;
    }
    else if (component == 1)
    {
        YJCityModel *cityModel = self.cities[row];
        label.text = cityModel.city;
        
    }else
    {
        YJCountyModel *countyModel = self.counties[row];
        label.text = countyModel.county;
    }
    return label;
}

//选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        
    }else if(component == 1)
    {
        [pickerView reloadComponent:2];
    }
}


#pragma mark ----------------------- 点击触发事件
- (void)show
{
    UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
    [win addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        _toolBar.transform = CGAffineTransformMakeTranslation(0, - PICKERVIEWH - TOOLBARH);
        _pickerView.transform = CGAffineTransformMakeTranslation(0,  - (PICKERVIEWH + TOOLBARH));
    }];
}

- (void)cancelBtnClick
{
    [self dismiss];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
        _toolBar.transform = CGAffineTransformIdentity;
        _pickerView.transform = CGAffineTransformIdentity;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//完成点击事件
- (void)completeBtnClick
{
    //省
    NSInteger selectProvinceIndex = [self.pickerView selectedRowInComponent:0];
    YJDistrictModel *districtModel = self.districts[selectProvinceIndex];
    NSString *province = districtModel.province;
    //市
    NSInteger selectCityIndex = [self.pickerView selectedRowInComponent:1];
    YJCityModel *cityModel = districtModel.cities[selectCityIndex];
    NSString *city = cityModel.city;
    //县
    NSInteger selectConutyIndex = [self.pickerView selectedRowInComponent:2];
    YJCountyModel *countyModle = cityModel.counties[selectConutyIndex];
    NSString *county = countyModle.county;
    
    NSString *district = [NSString stringWithFormat:@"%@ %@ %@",province,city,county];
    
    NSArray *arrs = @[district,province,city,county];
    if (self.choiceCityBlock)
    {
        self.choiceCityBlock(arrs);
    }
   
    
    if (self.completeBlock)
        self.completeBlock(district);
    
    [self dismiss];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return (IPHONE_W - 30)/3.0;
}



@end
